self.onmessage = function (e) {
  const n = e.data
  const startTime = performance.now()

  let tsp = new Date().getTime()

  // 模拟进度更新
  const updateProgress = () => {
    const ts = new Date().getTime()
    if (ts - tsp < 100) return
    tsp = ts
    const elapsed = performance.now() - startTime
    const progress = Math.min(95, (elapsed / 100) % 95)
    self.postMessage({ type: 'progress', progress: progress })
  }

  const fibonacci = n => {
    updateProgress()
    if (n <= 1) return n
    return fibonacci(n - 1) + fibonacci(n - 2)
  }

  // const progressInterval = setInterval(updateProgress, 100)

  // 执行计算
  const result = fibonacci(n)
  const endTime = performance.now()
  const time = endTime - startTime
  self.postMessage({ type: 'progress', progress: 100 })

  // clearInterval(progressInterval)

  // 发送结果回主线程
  self.postMessage({
    result: result,
    time: time
  })
}
